﻿using System.IO;
using System.Windows.Controls;

using Game.Level;
using Game.Serializer;

namespace Game.Editor
{
    class CampaignTreeControlPersister
    {
        static readonly Microsoft.Win32.OpenFileDialog _OpenFileDialog;
        static readonly Microsoft.Win32.SaveFileDialog _SaveFileDialog;

        readonly CampaignTreeControl _CampaignTreeControl;

        static CampaignTreeControlPersister()
        {
            _SaveFileDialog = new Microsoft.Win32.SaveFileDialog();
            _SaveFileDialog.Filter = "Campaign Files | *.campaign.xml";
            _SaveFileDialog.DefaultExt = "game.xml";

            _OpenFileDialog = new Microsoft.Win32.OpenFileDialog();
            _OpenFileDialog.Filter = "Campaign Files | *.campaign.xml";
        }

        public CampaignTreeControlPersister(CampaignTreeControl control)
        {
            _CampaignTreeControl = control;
        }

        public void Save(GameLevels levels)
        {
            Stream fileStream = null;
            if (_CampaignTreeControl._Filename == null)
            {
                bool? result = _SaveFileDialog.ShowDialog();
                if (result != null && result.Value)
                {
                    fileStream = _SaveFileDialog.OpenFile();
                    _CampaignTreeControl._Filename = _SaveFileDialog.SafeFileName;
                }
            }
            else
            {
                fileStream = new FileStream(
                    _CampaignTreeControl._Filename, FileMode.Truncate);
            }

            if (fileStream == null)
            {
                return;
            }

            StreamWriter sw = new StreamWriter(fileStream);
            GameMapSerializer.Save(_CampaignTreeControl.Root.GameLevels, fileStream);

            sw.Flush();
            sw.Close();
            fileStream.Close();
        }

        public void LoadGame()
        {
            GameLevels levels = new GameLevels();

            bool? result = _OpenFileDialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                Stream stream = _OpenFileDialog.OpenFile();
                levels = GameMapSerializer.Load(stream);

                _CampaignTreeControl.Close();
                _CampaignTreeControl.CreateRoot();

                _CampaignTreeControl.Root = new CampainTreeLevelsItem(levels);
                AddTreeItems(levels);
            }

        }

        void AddTreeItems(GameLevels levels)
        {
            foreach (GameLevel level in levels)
            {
                CampainTreeLevelItem item = 
                    new CampainTreeLevelItem(
                        _CampaignTreeControl._Root, level);
                _CampaignTreeControl._Root.Items.Add(item);

                foreach (GameMilestone milestone in level.Milestones)
                {
                    CampainTreeMilestoneItem item2 = 
                        new CampainTreeMilestoneItem(item, milestone);
                    item.Items.Add(item2);
                }
            }
        }

    }

}
